package com.guaju.yueshangLife.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.GoodsDetailActivity;
import com.guaju.yueshangLife.bean.CartGoods;
import com.guaju.yueshangLife.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AYD on 2016/11/21.
 * <p/>
 * 购物车Adapter
 */
public class ShoppingCartAdapter extends BaseAdapter {

    private boolean isShow = true;//是否显示编辑/完成
    private List<CartGoods> shoppingCartBeanList;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;
    private Context context;

    public ShoppingCartAdapter(Context context) {
        this.context = context;
    }

    public void setShoppingCartBeanList(List<CartGoods> shoppingCartBeanList) {
        this.shoppingCartBeanList = shoppingCartBeanList;
        notifyDataSetChanged();
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getCount() {
        return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return shoppingCartBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 是否显示可编辑
     *
     * @param flag
     */
    public void isShow(boolean flag) {
        isShow = flag;
        notifyDataSetChanged();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CartGoods shoppingCartBean = shoppingCartBeanList.get(position);
        boolean choosed = shoppingCartBean.isChoosed();
        if (choosed){
            holder.ckOneChose.setChecked(true);
        }else{
            holder.ckOneChose.setChecked(false);
        }
        String attribute = shoppingCartBean.getAttribute();
        if (!StringUtil.isEmpty(attribute)){
            holder.tv_goods_remain.setText(attribute);
        }else{
            holder.tv_goods_remain.setText(shoppingCartBean.getDressSize()+"");
        }
        holder.tv_goods_name.setText(shoppingCartBean.getTitle());
        holder.tvCommodityPrice.setText(shoppingCartBean.getPrice()+"");
        holder.tvCommodityNum.setText(shoppingCartBean.getCount()+"");
        holder.tvCommodityShowNum.setText(shoppingCartBean.getCount()+"");
        holder.ivShowPic.setImageURI(shoppingCartBean.getImageUrl());
        //单选框按钮
        holder.ckOneChose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shoppingCartBean.setChoosed(((CheckBox) v).isChecked());
                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );
        //增加按钮
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doIncrease(position, holder.tvCommodityShowNum, holder.ckOneChose.isChecked());//暴露增加接口


            }
        });
        //删减按钮
        holder.ivSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position, holder.tvCommodityShowNum, holder.ckOneChose.isChecked());//暴露删减接口
            }
        });

        holder.ckOneChose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    modifyCountInterface.onDelChecked(position);
                }else{
                    modifyCountInterface.onDelUnChecked(position);
                }
            }
        });


//        //删除弹窗
//        holder.tvCommodityDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                AlertDialog alert = new AlertDialog.Builder(context).create();
//                alert.setTitle("操作提示");
//                alert.setMessage("您确定要将这些商品从购物车中移除吗？");
//                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                return;
//                            }
//                        });
//                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                modifyCountInterface.childDelete(position);//删除 目前只是从item中移除
//
//                            }
//                        });
//                alert.show();
//            }
//        });
//        //判断是否在编辑状态下
//        if (isShow) {
//            holder.tv_goods_name.setVisibility(View.VISIBLE);
//            holder.rlEdit.setVisibility(View.GONE);
//            holder.tvCommodityNum.setVisibility(View.VISIBLE);
//            holder.tvCommodityDelete.setVisibility(View.GONE);
//        } else {
//            holder.tv_goods_name.setVisibility(View.VISIBLE);
//            holder.rlEdit.setVisibility(View.VISIBLE);
//            holder.tvCommodityNum.setVisibility(View.GONE);
//            holder.tvCommodityDelete.setVisibility(View.VISIBLE);
//        }
//            holder.ivShowPic.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    CartGoods cartGoods = shoppingCartBeanList.get(position);
//                    Intent intent = new Intent(context,GoodsDetailActivity.class);
//                    intent.putExtra(GoodsDetailActivity.GOODS_ID,cartGoods.getId()+"");
//                    context.startActivity(intent);
//                }
//            });
        return convertView;
    }
    //初始化控件
    class ViewHolder {
        ImageView tvCommodityDelete;
        SimpleDraweeView ivShowPic;
        TextView tv_goods_name, tv_goods_remain,tv_selected_info, tvCommodityPrice, tvCommodityNum, tvCommodityShowNum,tv_discount_info,get_discount_coupon,tv_promotion_info;
        CheckBox ckOneChose;
        LinearLayout rlEdit;
        View ivSub,ivAdd;
        public ViewHolder(View itemView) {
            ckOneChose = (CheckBox) itemView.findViewById(R.id.ck_chose);
            ivShowPic = (SimpleDraweeView) itemView.findViewById(R.id.iv_show_pic);
            ivSub =  itemView.findViewById(R.id.iv_sub);
            ivAdd =  itemView.findViewById(R.id.iv_add);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_goods_remain = (TextView) itemView.findViewById(R.id.tv_goods_remain);
            tv_selected_info = (TextView) itemView.findViewById(R.id.tv_selected_info);
            tv_discount_info = (TextView) itemView.findViewById(R.id.tv_discount_info);
            get_discount_coupon = (TextView) itemView.findViewById(R.id.get_discount_coupon);
            tv_promotion_info = (TextView) itemView.findViewById(R.id.tv_promotion_info);



            tvCommodityPrice = (TextView) itemView.findViewById(R.id.tv_commodity_price);
            tvCommodityNum = (TextView) itemView.findViewById(R.id.tv_commodity_num);
            tvCommodityShowNum = (TextView) itemView.findViewById(R.id.tv_commodity_show_num);
            rlEdit = (LinearLayout) itemView.findViewById(R.id.rl_edit);
        }
    }
    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         *
         * @param list 删除列表
         */
        void childDelete(ArrayList<Integer> list);

        void onDelChecked(int position);

        void onDelUnChecked(int position);
    }
}
